package cn.wen.base.adapter;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.TextView;

import cn.wen.base.image.ImageLoader;


/**
 *
 */
public class QuickViewHolder {
    private SparseArray<View> mViews;
    private View itemView;
    private int mLayoutId;


    public QuickViewHolder(LayoutInflater mInflater, ViewGroup parent, int layoutId) {
        itemView = mInflater.inflate(layoutId, parent, false);
        this.mLayoutId = layoutId;
        mViews = new SparseArray<>();
        // 给ViewHolder打上TAG
        itemView.setTag(this);
    }

    public int getLayoutId() {
        return mLayoutId;
    }

    public View getConvertView() {
        return itemView;
    }

    /**
     * 设置条目的点击事件
     */
    public QuickViewHolder setOnClickListener(View.OnClickListener listener) {
        itemView.setOnClickListener(listener);
        return this;
    }

    /**
     * 设置条目的长按事件
     */
    public QuickViewHolder setOnLongClickListener(View.OnLongClickListener listener) {
        itemView.setOnLongClickListener(listener);
        return this;
    }

    /**
     * 设置View的点击事件
     *
     * @return
     */
    public QuickViewHolder setOnClickListener(int viewId, View.OnClickListener listener) {
        View view = getView(viewId);
        if (view != null) {
            view.setOnClickListener(listener);
        }
        return this;
    }

    /**
     * 获取条目的View
     */
    public View getView() {
        return itemView;
    }

    /**
     * 根据ID获取条目里面的View
     */
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public QuickViewHolder setText(int viewId, CharSequence text) {
        TextView tv = getView(viewId);
        if (tv != null && !TextUtils.isEmpty(text)) {
            tv.setText(text);
        }
        return this;
    }

    /**
     * 设置图片背景颜色
     */
    public QuickViewHolder setTextColor(int viewId, int color) {
        TextView view = getView(viewId);
        if (view != null) {
            view.setTextColor(color);
        }
        return this;
    }

    /**
     * 设置控件是否可见
     */
    public QuickViewHolder setVisible(int viewId, int visible) {
        View view = getView(viewId);
        view.setVisibility(visible);
        return this;
    }

    /**
     * 设置控件选中
     */
    public QuickViewHolder setChecked(int viewId, boolean checked) {
        Checkable view = getView(viewId);
        view.setChecked(checked);
        return this;
    }

    /**
     * 设置控件背景
     */
    public QuickViewHolder setBackgroundRes(int viewId, int backgroundRes) {
        View view = getView(viewId);
        view.setBackgroundResource(backgroundRes);
        return this;
    }

    /**
     * 设置图片
     */
    public QuickViewHolder setImageResource(int viewId, int imageResId) {
        ImageView imageView = getView(viewId);
        imageView.setImageResource(imageResId);
        return this;
    }

    /**
     * 设置图片
     */
    public QuickViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView imageView = getView(viewId);
        imageView.setImageBitmap(bitmap);
        return this;
    }

    /**
     * 根据URL加载图片
     */
    public QuickViewHolder displayImage(int viewId, String url, int errorResId) {
        ImageView imageView = getView(viewId);
        ImageLoader.displayImage(imageView, url, errorResId);
        return this;
    }

    /**
     * 根据URL加载图片
     */
    public QuickViewHolder displayImage(int viewId, String url) {
        ImageView imageView = getView(viewId);
        ImageLoader.displayImage(imageView, url);
        return this;
    }

    /**
     * 根据本地资源ID加载图片
     */
    public QuickViewHolder displayImage(int viewId, int resId) {
        ImageView imageView = getView(viewId);
        ImageLoader.displayImage(imageView, resId);
        return this;
    }

    /**
     * 根据URL加载圆形图片
     */
    public QuickViewHolder displayCircleImage(int viewId, String url, int errorResId) {
        ImageView imageView = getView(viewId);
        ImageLoader.displayCircleImage(imageView, url, errorResId);
        return this;
    }

    /**
     * 根据URL加载圆角图片
     */
    public QuickViewHolder displayRadiusImage(int viewId, String url, int errorResId, float radius) {
        ImageView imageView = getView(viewId);
        ImageLoader.displayRadiusImage(imageView, url, errorResId, radius);
        return this;
    }


}
